const jwt = require("jsonwebtoken");
const util = require("util");
const verify = util.promisify(jwt.verify);

module.exports = async (ctx, next) => {
  // 获取jwt
  let tokenStr = ctx.header["token"];
  const token = "Bearer " + tokenStr;
  if (!!tokenStr) {
    try {
      let payload = await verify(token.split(" ")[1], "finance");
      ctx.user = payload;
    } catch (error) {
      ctx.user = {
        status: 1,
        msg: 'token已失效,请重新登录'
      };
    }
  } else {
    ctx.user = {
      status: 0,
      msg: 'token不存在,请先登录'
    };
  }
  await next();
};
